<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ApiserverModel
 *
 * @author NE
 */

class Backend_ApiserverModel extends RCMS_Core_BaseModel {

	public function getWebsitePages($id = 0) {
		$sitePages = array();
		$select = $this->getAdapter()->select()
			->from(
				array('p' => 'page'),
				array(
					'categoryId'        => 'category_id',
					'navigationTitle'   => 'nav_name',
					'description'       => 'short_description',
					'metaDescription'   => 'meta_description',
					'metaKeyWords'      => 'meta_keywords',
					'headerTitle'       => 'header_title',
					'is404Page'         => 'is_404page',
					'useBestPracticies' => 'bestpractices',
					'showInMenu'        => 'show_in_menu',
					'staticOrder'       => 'static_order',
					'ruleId'            => 'seosamba_rule_id',
					'targetedKeyPhrase' => 'targeted_key_phrase',
					'templateId'        => 'template_id',
					'rawContent'        => 'raw_content',
					'url',
					'order',
					'h1',
					'id'
				)
			)
			->joinLeft(
				array('c' => 'category'),
				'p.category_id=c.id',
				array(
					'categoryName' => 'name',
					'parentCatId'  => 'id_parent'
				)
			)
			->where('c.id_parent=?', $id)
			->where('p.protected="0"');
		$pages =  $this->_adapter->fetchAll($select, null, Zend_Db::FETCH_ASSOC);
		return $pages;
	}

	public function selectTemplateNameById($templateId) {
		$select = $this->getAdapter()->select()
			->from(
				array('t' => 'template'),
				'name'
			)
			->where('t.id = ?', $templateId);
		return $this->getAdapter()->fetchOne($select);
	}

	public function selectCurrentProductTemplate() {
		$select = $this->getAdapter()->select()
			->from(
				array('sc' => 'shopping_config'),
				array('value')
			)
			->where('name = \'template_product\'');
		return $this->getAdapter()->fetchOne($select);
	}

	public function saveProductTemplate($productTemplate) {
		$data = array(
			'value' => $productTemplate
		);
		$where = sprintf("name='template_product'");
		return $this->getAdapter()->update('shopping_config', $data, $where);
	}

	public function getDeeplinks() {
		$select = $this->getAdapter()->select()
			->from(
				array('d' => 'deeplink'),
				array(
					'name',
					'url',
					'type',
					'ban',
					'nofollow',
					'extWebsiteId' => 'ext_ws_id'
				)
			);
		return $this->getAdapter()->fetchAll($select, null, Zend_Db::FETCH_ASSOC);
	}

	public function getPageIdByDeeplinkUrl($url) {
		$select = $this->getAdapter()->select()
        ->from(
            array('p' => 'page'),
            array(
                    'id',
            )
        )
        ->where('p.url="?"', $url);
		$result = $this->getAdapter()->fetchOne($select, null, Zend_Db::FETCH_ASSOC);
		if($result) {
			return $result;
		}
		return 0;
	}

	public function getRedirects() {
		$select = $this->getAdapter()->select()
			->from(
				'renamed_pages',
				array(
					'original_old_url',
					'new_url'
				)
			);
		return $this->getAdapter()->fetchPairs($select);
	}

	public function getOldUrlByNewUrl($newUrl) {
		$select = $this->getAdapter()->select()
			->from(
				'renamed_pages',
				array(
					'old_url'
				)
			)
			->wehre($this->getAdapter()->quoteInto('new_url=?', $newUrl));
		return $this->getAdapter()->fetchOne($select);
	}

}

